package com.example.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.admin.entity.SysDictInfo;
import com.example.admin.entity.SysDictType;
import com.example.admin.mapper.SysDictTypeMapper;
import com.example.admin.service.ISysDictInfoService;
import com.example.admin.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wangkai
 * @since 2019-06-04
 */
@Service("sysDictTypeService")
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    @Autowired
    private ISysDictInfoService infoService;

    @Override
    @Transactional(readOnly = false, rollbackFor = {Exception.class, RuntimeException.class})
    public boolean deleteType(List<Integer> ids) {
        if (this.removeByIds(ids)) {
            Collection<SysDictType> list = this.listByIds(ids);
            if(list!=null&&list.size()>0){
                QueryWrapper<SysDictInfo> queryWrapper = new QueryWrapper<>();
                queryWrapper.in("dict_type", list.stream().map(info->info.getDictType()).collect(Collectors.toList()));
                if (!infoService.remove(queryWrapper)) {
                    throw new RuntimeException();
                }
            }
        }
        return true;
    }

}
